package com.example.demo.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.example.demo.Res.Res;
import com.example.demo.entity.Menu;
import com.example.demo.service.MenuService;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/menu/")
public class MenuController {

    private MenuService menuService;

    public MenuController(MenuService menuService) {
        this.menuService = menuService;
    }


    @GetMapping("list")
    @CrossOrigin
    Res find(@RequestParam String userId) {
        return Res.litOk("", menuService.selectMenuByUserId(userId));
    }

    @CrossOrigin
    @PostMapping("add")
    Res add(@RequestBody Menu menu) {
        final int i = menuService.addMenu(menu);
        if(i == -1) {
            return  Res.bigOn("网址或者路由名称重复", null);
        }
        return i > 0 ? Res.bigOk("菜单添加成功", null) : Res.litNo("菜单添加失败", null);
    }

    @CrossOrigin
    @GetMapping("selectOne")
    Res one(@RequestParam String id) {
        final Menu menu = menuService.selectMenuOneById(id);
        if(menu != null) {
            return Res.bigOk("", menu);
        } else {
            return Res.bigOn("获取失败", null);
        }
    }

    @CrossOrigin
    @PostMapping("update")
    Res ups(@RequestBody Menu menu) {
        final int i = menuService.updateMenu(menu);
        return i > 0 ? Res.bigOk("修改成功", null) : Res.litNo("修改失败", null);
    }

    @CrossOrigin
    @GetMapping("all")
    Res all(@RequestParam String title, @RequestParam String menuName) {
        final Object o = menuService.selectAll(title, menuName);
        final Res res = Res.litOk("", o);
        return res;
    }

    @CrossOrigin
    @PostMapping("delete")
    Res ups(@RequestParam String id) {
        final int i = menuService.delete(id);
        return i > 0 ? Res.bigOk("删除成功", null) : Res.litNo("删除失败", null);
    }

    @CrossOrigin
    @GetMapping("selectSelect")
    Res all2() {
        return Res.litOk("", menuService.selectSelect());
    }
}
